All right.
And the last video we created.
The equal weighted portfolio.
And in this video we are going to create many random portfolios consisting of our six stocks.
So we want to have kind of a cloud here with random portfolios and random means so that we have randomly
generated.
The weights of the constituents and the only constraint or assumption is that each single way is between
0 and 1.
So there are no short positions and of course the weights of all six stocks have to sum up to one.
So let's start here and we still have defined our user defined function annualized the risk and return
where we can transform a return data frame into a summary data frame with the annualized risk and return.
And so let's start here.
We import panda's name pi and met plop lip and also seaborne and again we are importing and working
with our stocks the portfolio stocks data frame.
So this is nothing new here.
And then in the next step we create actually the returns data frame with the percentage change method
so here we have our returns data frame Staley returns and bypassing our returns data frame to our user
defined function annualized risk and return.
We can simply create our summary data frame so let's have a look so here we have the annualized return
and risk for a six stocks and now we want to create random portfolios.
And first of all we have to define the number of constituents.
So here in our example these six stocks and we save six to the variable number of assets so we have
six.
And then we define that in our first simulation we want to simulate 10 portfolios.
So a number of portfolios of random portfolios is 10 so we have in total 10 random portfolios and in
each portfolios we have our six stocks.
So in total we have 60 a random weights and we can actually create random numbers with the name pi dot
random module.
So if we apply here the random module and the free selected the method random.
So let's have a look here.
So the random method returns a Random float in the half open interval between 0 and 1 and 2 we find
that we want to have a 60 a random numbers.
So let's have a look here
so here we have 60 random floats between 0 and 1.
And as we have 10 portfolios with each uh six stocks we can also then reshape here or a nun pie array
to 10 rows which are the portfolios and 6 columns and we can do this with the reshape method.
So here we have an array with 10 rows.
So one two three and 10 I was 10 rows and in each row we have uh six elements.
So we have six columns.
So the first second third fourth fifth and sixth.
So these are our random numbers.
And the fear Rita run here the cell and recreate our random numbers then we should get different numbers.
So here the very first numbers 0 point 5 2 and if you rerun here the cell we get 0 point 7 3.
And if you work with random numbers that might be desirable to have some kind of reproducibility and
we can do this by setting a random seed.
So we are using here the seed method and we pass an integer as well for example 1 2 3 and then in the
next step when we are creating random numbers.
So whenever we have a set of the same seed then we get the identical random numbers.
So to say upside or random numbers.
So first of all we set CAC to off one two three.
And then again we're creating here a num pi matrix with 10 rows and six columns.
So we are using the random method and we pass an always on number of assets.
Times number of portfolios.
So this is six times ten.
And then we reshape to 10 rows and six columns and then we actually saving the NUM pi array and the
rabble matrix.
So let's do this and let's have a look.
So this our 60 random floats.
And if you rerun the code here we should exactly get the same random numbers as we set a seed.
So let's try this out.
So here the very first numbers 0 point 6 9 and yet still 0 point 6 9.
So here we have our 10 portfolios with uh each six weights.
And we can easily see here that debates that do not sum up to a one.
So here we have the first portfolio.
And here we have 69 percent 71 percent.
So we are far away from a sum of weights of one.
However we can simply normalize the weights here and get the weights that sum up to 1 and the first
of all that's created the sum of weights for each portfolio and we can do this by applying the sum method
and pass one to the axis pair meter.
So we want to have the sums for all rows and we actually pass true to the keep them parameter to have
an umpire array.
Still with 10 rows and one column so let's have a look.
So this the sums for the rose so far the first row.
We have a sum of two point nine and get actually rates that sum up to one.
We simply have to divide each element here in one portfolio on one row by the sum of the row.
So for example the sum of the first row is two point nine.
And we have to divide each single element in our first row by two point nine.
And by doing so we are normalizing here our values and the values that then should sum up to 1.
So let's do this year.
So we're dividing our matrix by the sum of the rows of the matrix and then we are saving actually the
resulting matrix with weights and all rows that sum up to 1 in the variable rates.
So let's do this and let's have a look so here we have again our first portfolio.
And here we have a point two three 0 point too far.
So this pretty looks good actually.
And we can also prove that in each portfolio the weights sum up to 1.
So again we use here the sum method and we can see here that needs a single row for each single portfolio.
We have a total sum up a weight of 11.
So this is exactly what we need and now we can calculate the re weighted average daily return for each
random portfolio and for each time stamp with the top method.
So we use our returns data frame and we pass Yeah our transposed weights num pi array to the top method.
And by doing so we're creating a data frame with portfolio returns for our 10 portfolios.
And actually we save the portfolio returns on the variable part red.
So let's do this and let's have a look so here he CFS columns our 10 random portfolios starting from
0 to 9 and still we have each timestamp.
And here we have actually the daily returns for all of our random portfolios and now in the next step
we can also create the summary data frame of our random portfolios by passing here.
The portfolio returns to the annualized the risk return user defined function.
So let's do this and let's have a look so here we have now on the left hand side as the index our random
portfolios from 0 to 9 and we have actually the annualized the returns and the annualized the risk for
each and every random portfolio here.
And finally we can also plot our random portfolios and create a set up plot.
So first of all we create a scatter plot for our Random portfolios with actually the risk on the x axis
and the return on the y axis.
And we also plotted our six stocks with the risk on the x axis and the return on the y axis.
And actually our random portfolios have a size of 20.
And a red color and our stocks have a higher size of 50 year black colour and a marker of the year.
So Diamond so let's have a look here
so here you have now 10 random portfolios and this looks quite nice.
However if you want to perform a deeper analysis we need a lot of more random portfolios and we can
do so by simply increasing the number of portfolios.
Let's go up again here
so here we have the numbers of portfolios.
And for example we could simulate and set of 10.
We could simulate 100 a thousand random portfolios.
So let's throw this here.
And what we simply have to do now we have to rerun our code.
So starting with this line here.
So we are creating 100000 portfolios each consisting of our six stocks with random weights
so let's again normalize the weights to have a sum of 1 and let's check it here so for each portfolio
we FBA some of weights of 1 and let's create the daily returns for each and every random portfolio
so here we have our random portfolios starting with the very first one and ending at portfolio one hundred
thousand and then we also create the annualized risk and return for each and every portfolio by passing
year the portfolio return to our user defined function.
So this is the most time consuming operation here and it could take a while so let's simply wait here
so we are finished and here we have the annualized risk and return for all 100000 portfolios
so let's have a look down here.
So it's here until we have fear of 100000 portfolios and the then we can create here our cloud of portfolios.
So let's say this
and if we can see and read our 100000 portfolios in black here we have the risk return profile of our
six constituent stocks.
So this looks pretty cool.
And in the next video we are going to analyze our 100000 portfolios so hope to see there by.